 
<?php
    error_reporting(E_ALL);
include_once('controller/includes/db_access.php');
function dateformat($param) 
{
  // format "2012-06-19"  
  $dt = strftime("%Y-%m-%d", strtotime($param));  
   // convertir les accents (pour encodage UTF-8)  
  $dt = mb_convert_encoding($dt, 'utf-8'); 
   return $dt;
}
// kijk of de radio button is geselecteerd./////////////////////////////
if($_POST['dt'] == 'dt' ){
//1 post datum from stap1
$van_datum=dateformat($_POST['from']);  
$tot_datum=dateformat($_POST['to']);

     
 //1.1 count aantal dagen
 function daysDifference($endDate, $beginDate)
    {
    //explode the date by "-" and storing to array
       $date_parts1=explode("-", $beginDate);
       $date_parts2=explode("-", $endDate);
       //gregoriantojd() Converts a Gregorian date to Julian Day Count
       $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
       $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
       return $end_date - $start_date;
    }
     $total_day = daysDifference($tot_datum, $van_datum);
}// eind $datum =($_POST['dt']);
 
    // die('datum'.$datum. $total_day );
if($_POST['dt'] == 'gdt' ) $total_day = $_POST['aantal'];
    //2 check beschikbaar per dag
 $beschikbaarheid = 'TRUE';
 
 $product_not_available= array();
  $set_not_available= array();
  $begin_dag= $van_datum;
  for($i=1; $i <= $total_day; $i++ )
   {
      
    $SQL_query = "SELECT P.aantal AS aantal_in_sys, S2P.aantal AS aantal_in_set, S2P.product_id, S2P.set_id FROM product P, set2product S2P WHERE P.product_id = S2P.product_id";
    $query_set2product = mysql_query($SQL_query);
    while ($row_set2product = mysql_fetch_object($query_set2product)) 
        { 
            $set_id=$row_set2product->set_id;
            //welke product zit in de set
            $product_in_set=$row_set2product->product_id;
             //aantal per product in de set
            $aantal_in_set = $row_set2product->aantal_in_set;
            
             //aantal per product in de systeem
            $aantal_in_sys = $row_set2product->aantal_in_sys;
     
  

            //2.2 count aantal per product inhuur van dat set op die datum 

            

                //$begin_dag = $van_datum;
              
                
                $sql_inhuur = "SELECT SUM(aantal) AS totaal FROM beschikbaarheid WHERE datum='$begin_dag' AND product_id=".$product_in_set;
                $resultInhuur = mysql_query($sql_inhuur);
                $rowsInhuur = mysql_fetch_object($resultInhuur);
                // totaal per product inhuur 
                $totaalBeschikbaar=$rowsInhuur->totaal;
                
                 //2.3 kijk of het verchil van totaal product en het aantal inhuur > aan de product in  set 

                if((($totaalBeschikbaar + $aantal_in_set) > $aantal_in_sys))
                    {
                    
                        $product_not_available[$product_in_set]='FALSE';
                        $set_not_available[$set_id]='FALSE';
                        $beschikbaarheid = 'FALSE';
                        //exit ();
                        //if ($beschikbaarheid == 'FALSE') die($begin_dag .' - ' .$totaalBeschikbaar.' - '. $aantal_in_set.' - '.$aantal_in_sys);
                    }//EIND VAN DE if conditie
                          
                  
              
                    
            }// eind van het while loop
            
              //Add one day to  current date
                $date = strtotime(date("Y-m-d", strtotime($begin_dag)) . " +1 day");
                $begin_dag = date('Y-m-d', $date);

         }//EIND VAN DE for LOOP
      


    if (isset($_POST['toepassing']) && $_POST['toepassing']>0 ) 
        {
           $SQLset2toepassing ="SELECT * FROM sets S, set2toepassing S2T WHERE S.set_id = S2T.set_id AND toepassing_id=". $_POST['toepassing']. " GROUP BY S.set_id";
           $query_set2product = mysql_query($SQLset2toepassing);
         
         } 
         else
         {
           $query_set2product = mysql_query('SELECT * FROM sets S, set2product S2P WHERE S.set_id = S2P.set_id GROUP BY S.set_id ORDER BY setprijs ASC');   
         }           

?>
<script type="text/javascript">
function OnSubmitForm()
{
  if(document.pressed == 'volgende')
  {
   document.myform.action ="stap3.php";
  }
 
  return true;
}
</script>
<form name="myform" method="post" onsubmit="return OnSubmitForm();">
<div align="center">
    
  <input type="hidden" name="van" value="<?php echo $_POST['from']; ?>" />
  <input type="hidden" name="tot" value="<?php echo $_POST['to']; ?>" />
  <input type="hidden" name="dt" value="<?php echo $_POST['dt']; ?>" />
    <input type="hidden" name="aantal" value="<?php echo $_POST['aantal']; ?>" />

                  <h1>2. Selecteer uw geluidsset</h1>



                <div class="content">
                    <table border="0">  
                        
                    <tr>
                         <td colspan="3"> Toon: 
                         <select name="toepassing" onchange="document.forms['myform'].submit();">
                              <option>alle sets(of kies hier uw toepassing)</option>
                       <?php
                   
// put your code here
                $q = new Query;
                $q
                        ->select('*'
                        )
                        ->from('`toepassingen`')
                        ->order_by(
                                array(
                                    'toepassingen.sort ASC',
                                )
                        )
                        ->run();

                $result = $q->run();
                $count = $q->get_selected_count();

                if (!($result && $count > 0)) {
                    echo 'No users found.' . "\n";
                } else {
                    while ($rows = mysql_fetch_object($result)) {
                        
                        $selected=$_POST['toepassing']==$rows->toepassing_id?' selected':'';
                        echo' <option value="'.$rows->toepassing_id.'"'.$selected.'>'.$rows->sort.'</option>';
                    }
                }
                ?>

                      </select>
                      </td>
                  </tr> 
                              <tr>
                                  <td colspan="3" ><input type="radio" name="set_id" value="" />Geen complete set nodig </td>
                              </tr>
                    <?php
                 
                    
                    while ($rows_set2product = mysql_fetch_object($query_set2product)) {
                                
                        $resultproduct = mysql_query('SELECT * FROM sets WHERE set_id='.$rows_set2product->set_id);
                         while ($rowsproduct = mysql_fetch_object($resultproduct)) 
                            {
                                 
                                $disabled= array_key_exists($rowsproduct->set_id,$set_not_available).$rowsproduct->set_id;       
                            
                            }
                            $detoepassing='';
                         $resultset2toepassing = mysql_query('SELECT * FROM toepassingen T, set2toepassing S2T WHERE T.toepassing_id=S2T.toepassing_id AND S2T.set_id='.$rows_set2product->set_id);
                        while($rowsset2toepassing = mysql_fetch_object($resultset2toepassing))
                        
                                {
                                   $toepassing_id= $rowsset2toepassing->set_id;
                                   $detoepassing.= $rowsset2toepassing->sort.'&nbsp; &nbsp;';
                                }
                    ?>      
                     <tr>
                      <td valign="top"><input type="radio" required = "required" name="set_id" value="<?php echo $rows_set2product->set_id; ?>" <?php if($disabled == '1'.$rows_set2product->set_id) echo 'disabled="disabled"'; ?> />
                       <img src="my_uploads/sets/images/<?php echo $rows_set2product->foto; ?>" width="50" height="50"/>
                      </td>
                   <td valign="top">
                       
                           <?php echo $rows_set2product->naam; ?>&nbsp; &nbsp;<?php echo $detoepassing; ?>&nbsp; &nbsp;<br /><?php echo $rows_set2product->descr; ?>
                   </td>
           
                   <td valign="top">Huurprijs &euro;<?php echo $rows_set2product->setprijs; ?>
                   
                   </td>
                   </tr>
                       <?php 
                                   $resultproduct = mysql_query('SELECT * FROM product P, set2product S2P WHERE P.product_id = S2P.product_id and set_id='.$rows_set2product->set_id);
                                    while ($rowsproduct = mysql_fetch_object($resultproduct)) {
                                        ?> 


                                        <tr>
                                            
                                            <td></td>
                                            <td  valign="top"> <input type="hidden" name="<?php echo $rowsproduct->product_id; ?>" value="" />
                                              <?php echo $rowsproduct->aantal; ?> x <a href=""> <?php echo $rowsproduct->naam; ?></a> (<?php echo $rowsproduct->product_id; ?>) 
                                            </td>

                                            <td><?php  if (array_key_exists($rowsproduct->product_id,$product_not_available)) echo '<font color="red">Niet beschikbaar</font>'; ?></td>
                                        </tr>  

                                        <?php }?>
                          
                          <tr>
                              <td> </td>
                              <td  valign="top"><a href="">Download</a></td>
                              <td></td>
                          </tr>
 
                        <?php  } ?>
                    </table>
                <br/>
                        <input type="button" value="vorig stap"/>
                       <input type="submit" name="nextstap" onclick="document.pressed=this.value" value="volgende" />

                        
                </div>                

        </div>
</form>
<?php //} ?>